<?php 
	namespace app\admin\controller;
	use think\Db;
	class Leader extends Base{
		public function leaderList(){			
//			$data = Db::name("service_team")->select();
//			$count = count($data);
			$data = Db::name('leader')->order("leader_id desc")->paginate(5,false, [ 'type'=>'Bootstrap2' ] );
			$this->assign("num",$data->getCurrentPage()*5-5);
			$this->assign("sl_data",$data);
			return $this->fetch();
		}
		public function addLeader(){
			if($_POST){
				$pic = '';
				$e = 0;//没上传错误
				$data = array(
					'leader_name'=>$_POST['leader_name'],
				);
//				array(5) { ["name"]=> array(3) { [0]=> string(6) "a1.jpg" [1]=> string(6) "a2.jpg" [2]=> string(6) "a3.jpg" } 
//				["type"]=> array(3) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(10) "image/jpeg" } 
//				["tmp_name"]=> array(3) { [0]=> string(22) "C:\Windows\php4C64.tmp" [1]=> string(22) "C:\Windows\php4C65.tmp" [2]=> string(22) "C:\Windows\php4C66.tmp" } 
//				["error"]=> array(3) { [0]=> int(0) [1]=> int(0) [2]=> int(0) } ["size"]=> array(3) { [0]=> int(40994) [1]=> int(13416) [2]=> int(50346) } }
				$file = $_FILES['file'];
				if($file['name']!=null){
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
						$fileType = pathinfo($file['name'],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确
							
						}
						if($file['size']>$size){
							$e = -2;//上传图片大于2M
							
						}
						if($file['error']!=0){
							$e = -3;//其他错误
							
						}
										
				}else{
					$pic="default.png";
				}
				if($e==0){
					if($file['name']!=null){
						$filename = time().rand(0, 1000).$file['name'];							
						$info = move_uploaded_file($file['tmp_name'], "./upload/admin/leader/$filename");						
						$pic=$filename;
					}	
					$data['leader_pic'] = $pic;										
					$id = Db::name("leader")->insert($data,false,true);
					$this->redirect("leaderlist");
				}
			}
			return $this->fetch();
		}
		public function getLeader($name){
			$data = Db::name("leader")->field("leader_name,leader_id")
			->where("leader_name", "like","%$name%")->limit(5)->select();
//			echo Db::name('leader')->getLastSql();die;
			$str = "";
			foreach($data as $leader){
				$str .= "<div id='".$leader['leader_id']."' class='lea_list'>".$leader['leader_name']."</div>";
			}
			echo $str;die;
		}
		public function editLeader($id){
			$data = Db::name('leader')->where("leader_id",$id)->find();
			$this->assign("leader",$data);
			return $this->fetch();
		}
		public function saveLeader(){
			if($_POST){
				$pic = '';
				$e = 0;//没上传错误
				$data = array(
					'leader_name'=>$_POST['leader_name'],
				);
//				array(5) { ["name"]=> array(3) { [0]=> string(6) "a1.jpg" [1]=> string(6) "a2.jpg" [2]=> string(6) "a3.jpg" } 
//				["type"]=> array(3) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(10) "image/jpeg" } 
//				["tmp_name"]=> array(3) { [0]=> string(22) "C:\Windows\php4C64.tmp" [1]=> string(22) "C:\Windows\php4C65.tmp" [2]=> string(22) "C:\Windows\php4C66.tmp" } 
//				["error"]=> array(3) { [0]=> int(0) [1]=> int(0) [2]=> int(0) } ["size"]=> array(3) { [0]=> int(40994) [1]=> int(13416) [2]=> int(50346) } }
				$file = $_FILES['file'];
				if($file['name']!=null){
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
						$fileType = pathinfo($file['name'],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确
							
						}
						if($file['size']>$size){
							$e = -2;//上传图片大于2M
							
						}
						if($file['error']!=0){
							$e = -3;//其他错误
							
						}
										
				}
				if($e==0){
					if($file['name']!=null){
						$filename = time().rand(0, 1000).$file['name'];							
						$info = move_uploaded_file($file['tmp_name'], "./upload/admin/leader/$filename");						
						$data['leader_pic'] = $filename;	
					}															
					$id = Db::name("leader")->where("leader_id",$_POST['leader_id'])->update($data);
					$this->redirect("leaderlist");
				}
			}
		}

		public function delLeader($id){
			$data = Db::name('leader')->field("leader_pic")->where("leader_id",$id)->find();
			Db::name("leader")->where("leader_id",$id)->delete();
			$uri = "./upload/admin/leader/".$data['leader_pic'];
			if(file_exists($uri)&&$data['leader_pic']!='default.png');
			unlink($uri);		
			echo "<script>alert('数据已删除')；</script>";
			$this->redirect("leaderlist");
		}
	}
	
?>